# -*- coding: utf-8 -*-
# @Author: liuzhen
# @Date:   2018-08-15 14:25:37
# @Last Modified by:   Sparks_lz
# @Last Modified time: 2018-08-16 19:44:02


import functionsPkg
import os ,sys  
from PySide2.QtCore import *
from PySide2.QtWidgets import *  
from PySide2.QtGui import *  


class Path(object)  :  
    def __init__(self) :  
        self.scriptPath = os.path.dirname(__file__)  
        self.parentPath = os.path.dirname(self.scriptPath)  
        self.iconPath = os.path.join(self.parentPath, 'Icon')  
        self.checkIconPath = os.path.join(self.iconPath, 'stateCheckmark64.png')  


class drop_listWidget(QListWidget) :  
    def __init__(self, type, parent=None):
        super(drop_listWidget, self).__init__(parent)
        self.setAcceptDrops(True)
        self.setDragEnabled(True)
        # self.setIconSize(QSize(72, 72))

    def dragEnterEvent(self, event):  
        if event.mimeData().hasUrls:  
            event.accept()  
        else:  
            event.ignore()  

    def dragMoveEvent(self, event):  
        if event.mimeData().hasUrls:  
            event.setDropAction(Qt.MoveAction)  
            event.accept()  
        else:  
            event.ignore()  

    def dropEvent(self, event):  
        if event.mimeData().hasUrls:  
            event.setDropAction(Qt.CopyAction)  
            event.accept()  
            links = [str(i.toLocalFile()) for i in event.mimeData().urls()]   
            # for url in event.mimeData().urls():
            #     links.append(str(url.toLocalFile()))
            # self.emit(SIGNAL("dropped"), links)
            for link in links :  
                children = [self.item(i).text() for i in range(self.count())]  
                if link in children :  
                    pass  
                else :  
                    item = QListWidgetItem(link, self)
            # children = [self.item(i).text() for i in range(self.count())]
            # print children
        else:  
            event.ignore()

class file_dialog(QFileDialog) :  
    def __init__(self, new_name) :  
        super(file_dialog, self).__init__()
        self.name = new_name
        self.setWindowTitle("%s" % (self.name))


class main_widget(QWidget) :  
    width = 380
    height = 450
    def __init__(self) :  
        super(main_widget, self).__init__()  

        self.setMaximumWidth(main_widget.width)
        self.setMinimumWidth(main_widget.width)
        self.setMaximumHeight(main_widget.height)
        self.setMinimumHeight(main_widget.height)

        self.pathPkg = Path()
        self.basepath = os.path.expanduser('~')
        self.checkIcon = QIcon(self.pathPkg.checkIconPath)
        print self.checkIcon


        self.listWidget_dp = drop_listWidget(self)  
        self.pathLineEdit = QLineEdit()
        self.pathLineEdit.setText(self.basepath)
        self.filedlg = file_dialog("Output")
        self.browserPushButton = QPushButton("...")
        self.pathLabel = QLabel()
        self.pathLabel.setText('Path')
        self.browserPushButton.setMaximumWidth(30)
        self.filePool = QGroupBox()  
        self.clearButton = QPushButton('Clear')  
        self.runButton = QPushButton('Run')  
        self.checkbutton = QPushButton()  
        self.checkbutton.setIcon(self.checkIcon)

        self.browserLayout = QHBoxLayout()
        self.browserLayout.addWidget(self.pathLabel)
        self.browserLayout.addWidget(self.pathLineEdit)
        self.browserLayout.addWidget(self.browserPushButton)

        self.filePool.setTitle("    Shot pool")  
        self.filePool.setLayout(QVBoxLayout())  
        self.filePool.layout().addWidget(self.listWidget_dp)
        self.filePool.layout().addWidget(self.clearButton)

        self.button_layout = QVBoxLayout()  
        self.button_layout.addWidget(self.runButton)  
        self.button_layout.addWidget(self.checkbutton)
        self.button_layout.addStretch()  


        # self.fileLayout = QHBoxLayout()  
        # self.fileLayout.addWidget(self.filePool)
        # self.fileLayout.addLayout(self.button_layout)

        self.oplayout = QVBoxLayout()
        self.oplayout.addLayout(self.browserLayout)
        self.oplayout.addWidget(self.filePool)

        self.fileLayout = QHBoxLayout()  
        self.fileLayout.addLayout(self.oplayout)
        self.fileLayout.addLayout(self.button_layout)


        self.setLayout(self.fileLayout)  


        self.clearButton.clicked.connect(self.clear)  
        self.runButton.clicked.connect(self.run)  
        self.browserPushButton.clicked.connect(self.chooseDir)
        self.pathLineEdit.editingFinished.connect(self.changelineedit)


    def __setBasepath(self) :
        self.basepath = self.pathLineEdit.text()
        print self.basepath


    def fileDropped(self, fileList) :  
        for url in fileList:
            if os.path.exists(url):
                item = QListWidgetItem(url, self.listWidget_dp)
                item.setStatusTip(url)  


    def clear(self) :  
        self.listWidget_dp.clear()

    def run(self) :  
        fileList = [self.listWidget_dp.item(i).text() for i in range(self.listWidget_dp.count())]  
        mbfileList = [os.path.join(i, x) for i in fileList for x in os.listdir(i) if x.split('.')[-1] == 'mb']
        if len(fileList) == 0 :  
            print "Drag shot file to the shot pool."
            pass
        else :  
            functionsPkg.exportCamera(mbfileList, self.basepath)
            print "@@@@@@@@@@@@@@@@@@@@@\n@Everything is done!@\n@@@@@@@@@@@@@@@@@@@@@"

    def chooseDir(self) :  
        self.pathLineEdit.setText("%s" % (self.filedlg.getExistingDirectory()))
        self.__setBasepath()

    def changelineedit(self) :  
        self.__setBasepath()



















if __name__ == '__main__':

    app = QApplication(sys.argv)
    a = main_widget()
    a.show()
    sys.exit(app.exec_())
